package cn.ruihusoft.ql.core;

import com.dtflys.forest.http.ForestAddress;
import lombok.Getter;
import org.springframework.util.StringUtils;

import java.net.URI;
import java.net.URISyntaxException;

public class QlSdkInit {

    @Getter
    private static String clientId;
    @Getter
    private static String clientSecret;
    @Getter
    private static String serverHost;

    private static boolean init = false;

    public static void init(String serverHost, String clientId, String clientSecret) {
        QlSdkInit.serverHost = serverHost;
        QlSdkInit.clientId = clientId;
        QlSdkInit.clientSecret = clientSecret;

        try {
            URI uri = new URI(serverHost);
            ForestAddress address = null;

            String path = uri.getPath();
            if(StringUtils.hasText(path) && !"/".equals(path)) {
                address = new ForestAddress(uri.getScheme(), uri.getHost(), uri.getPort(),path);
            } else {
                address = new ForestAddress(uri.getScheme(), uri.getHost(), uri.getPort());
            }

            DymticAddressSource.setAddress(address);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }

        init = true;
    }
}
